package ac.uk.qmul.gemom.scenario;

import java.util.Vector;
import java.util.Hashtable;
import java.util.Enumeration;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

/* 
 * COPYRIGHT DISCLAIMER
 * Synopsis: All the source codes presented in this document are intellectual
 * properties of Queen Mary, University of London.
 * 
 * @year 2010
 * @author Peng Jiang, John Bigham, Jiayi Wu Jinfu Wang------------QMUL
 * @version 1.0 
 */

public class Subscription {
	
	// host indicates broker address/id, etc
	private String _host;
    
    private Hashtable<String, Vector<String>> _namespaces;

    public Subscription(String host) {
    	this._host = host;
    	_namespaces = new Hashtable<String, Vector<String>>();
    }

    public Hashtable<String, Vector<String>> getAllNamespace() {
        return _namespaces;
    }
    
    public Vector<String> getNamespace(String ns) {
    	ns = Util.stripSlash(ns);
    	return _namespaces.get(ns);
    }

    public boolean addNamespace(String ns) {
    	ns = Util.stripSlash(ns);
    	if( _namespaces.get(ns) == null){
    	    _namespaces.put(ns, new Vector<String>());
    	    return true;
    	}
    	else
    		return false;
    }
    

    public boolean hasNamespace(String ns) {
    	ns = Util.stripSlash(ns);
    	if( _namespaces.get(ns) != null){
    	    return true;
    	}
    	else
    		return false;
    }
    
    public boolean addTopic(String ns, String tp)
    {
    	ns = Util.stripSlash(ns); 
    	if(!hasNamespace(ns))
    		addNamespace(ns);
   
    	Vector<String> nss = _namespaces.get(ns);
    	if( nss != null){
    		if(!nss.contains(tp)){
    			nss.add(tp);
    		    return true;
    		}
    		else
    			return false;
    	}
    	else
    		return false;
    }
    
    
    public void removeAll(){
    	_namespaces.clear();
    }
    
    
    public void removeNamespace(String ns){
    	ns = Util.stripSlash(ns);
    	_namespaces.remove(ns);
    }

    public void removeTopic(String ns, String tp)
    {
    	ns = Util.stripSlash(ns);
    	Vector<String> nss = _namespaces.get(ns);
    	if( nss != null){
    		nss.remove(tp);
    	}
    }
    
    public int getSize()
    {
    	return _namespaces.size();
    }
    
    public int getNamespaceSize(String ns)
    {
    	ns = Util.stripSlash(ns);
    	Vector<String> nss = _namespaces.get(ns);
    	if( nss != null){
    		return nss.size();
    	}
    	else
    		return 0;
    }
    
    public DefaultMutableTreeNode buildTreeNode(){
		DefaultMutableTreeNode root = new DefaultMutableTreeNode("All subscription");
		
		Enumeration nsKeys = _namespaces.keys();
		
		while(nsKeys.hasMoreElements()){
			 String ns = nsKeys.nextElement().toString();
			 Vector<String> nss = _namespaces.get(ns);
			 DefaultMutableTreeNode sonRoot = new DefaultMutableTreeNode(ns);
			 root.add(sonRoot);
			 for(int j = 0; j < nss.size(); j++){
				sonRoot.add( new DefaultMutableTreeNode(nss.elementAt(j)) );
			 }
		}
    	return root;
    }
    

   
}



